﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Net;
using System.IO;

namespace bsitProject
{
    public partial class C2 : Form
    {
        public C2()
        {
            InitializeComponent();
        }

        //SAX/stream, write xml, read from database

        List<Supplier2> QuotationList = new List<Supplier2>();

        private void C2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'supplier2DatabaseDataSet.Product' table. You can move, or remove it, as needed.
            this.productTableAdapter.Fill(this.supplier2DatabaseDataSet.Product);
        }

        private void btnSumit_Click(object sender, EventArgs e)
        {
            dgvOutput.Select();

            SqlConnection conn = new SqlConnection
          ("server=localhost;initial catalog=D:\\SUPPLIER2DATABASE.MDF;integrated security=yes");

            try
            {
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.CommandText = "SELECT p.*, r.*"
                    + "FROM RFQDetail r ,Product p WHERE r.ItemID = p.ProductID";
                comm.Connection = conn;
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    //string ProductID = dr["ProductID"].ToString();
                    //string ItemID = dr["ItemID"].ToString();
                    //string Name = dr["Name"].ToString();
                    //string Manufacturer = dr["Manufacturer"].ToString();
                    //string Quantity = dr["Quantity"].ToString();
                    //string Category = dr["Category"].ToString();
                    //string Description = dr["Description"].ToString();
                    //string UnitPrice = dr["UnitPrice"].ToString();
                    //string AskingPrice = dr["AskingPrice"].ToString();

                    Supplier2 s2 = new Supplier2();
                    s2.ProductID1 = dr["ProductID"].ToString();
                    s2.ItemID1 = dr["ItemID"].ToString();
                    s2.Name1 = dr["Name"].ToString();
                    s2.Manufacturer1 = dr["Manufacturer"].ToString();
                    s2.Quantity1 = dr["Quantity"].ToString();
                    s2.Category1 = dr["Category"].ToString();
                    s2.Description1 = dr["Description"].ToString();
                    s2.UnitPrice1 = dr["UnitPrice"].ToString();
                    s2.AskingPrice1 = dr["AskingPrice"].ToString();

                    //tbxResult.Clear();
                    QuotationList.Add(s2);
                    tbxResult.Text += "Product ID:" + s2.ProductID1 + "\r\n" + "ItemID: " + s2.ItemID1 + "\r\n" + "Name: " + s2.Name1 + "\r\n" + "Manufacturer: " + s2.Manufacturer1 + "\r\n"
                        + "Quantity: " + s2.Quantity1 + "\r\n" + "Category: " + s2.Category1 + "\r\n" + "Description: " + s2.Description1 + "\r\n" + "Unit Price: " + s2.UnitPrice1 + "\r\n" + "ASking Price: " + s2.AskingPrice1 + "\r\n" + "\r\n";
                }

                dr.Close();
            }


            catch (Exception ex)
            {
                throw ex;
            }

            //    SqlCommand comm1 = new SqlCommand();
            //    comm1.CommandText = "SELECT * from RFQDetail WHERE ItemID = ItemID";
            //    comm1.Connection = conn; 
            //    SqlDataReader dr1 = comm1.ExecuteReader();             

            //    while (dr1.Read())
            //    {
            //        string RFQID = dr["RFQID"].ToString();
            //        string ItemID = dr["ItemID"].ToString();
            //        string Quantity = dr["Quantity"].ToString();
            //        string AskingPrice = dr["AskingPrice"].ToString();
            //    }
            //    SqlCommand comm1 = new SqlCommand();
            //    comm1.CommandText = "SELECT * from RFQDetail WHERE ItemID = ItemID";
            //    comm1.Connection = conn; 
            //    SqlDataReader dr1 = comm1.ExecuteReader();             


            //    dr1.Close();
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

            //    while (dr1.Read())
            //    {
            //        string RFQID = dr["RFQID"].ToString();
            //        string ItemID = dr["ItemID"].ToString();
            //        string Quantity = dr["Quantity"].ToString();
            //        string AskingPrice = dr["AskingPrice"].ToString();
            //    }



            //    dr1.Close();
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

            //////////////
            //WRITE XML//  
            /////////////

            string url = (@"D:\GenerateXML\QuotationS2.xml");

            // args[0] contains file name of xml file to be created
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter wrt = XmlWriter.Create(url, settings);
            wrt.WriteStartDocument();
            wrt.WriteStartElement("TheQuotation");
            wrt.WriteElementString("quotationID", "111");
            wrt.WriteElementString("sellerID", "4321");
            wrt.WriteElementString("buyerID", "9888");
            wrt.WriteElementString("RFQID", "01");
            wrt.WriteStartElement("QuotationItem");

            double itemstotalprice = 0.0;
            double shippingcharges = 0.0;
            double quotationtotal = 0.0;
            foreach (Supplier2 s2 in QuotationList)
            {
                decimal totalprice = Convert.ToInt32(s2.Quantity1) * Convert.ToDecimal(s2.UnitPrice1);
                wrt.WriteStartElement("item");
                wrt.WriteAttributeString("id", s2.ItemID1);
                wrt.WriteElementString("quantity", s2.Quantity1);
                wrt.WriteElementString("unitPrice", s2.UnitPrice1);
                wrt.WriteElementString("totalPrice", totalprice.ToString());
                wrt.WriteEndElement();

                //items total price
                itemstotalprice += Convert.ToDouble(totalprice);
                //shipping charges
                if (Convert.ToInt32(s2.Quantity1) < 50)
                {
                    shippingcharges += Convert.ToInt32(s2.Quantity1) * 1;
                }
                else
                {
                    shippingcharges += (Convert.ToInt32(s2.Quantity1) - 50) * 1.5;
                }
                //quotation total
                quotationtotal = itemstotalprice + shippingcharges;
            }
            wrt.WriteElementString("itemsTotalPrice", itemstotalprice.ToString());

            wrt.WriteElementString("shippingCharges", shippingcharges.ToString());

            wrt.WriteElementString("quotationTotal", quotationtotal.ToString());

            wrt.WriteEndElement();

            wrt.WriteEndElement();
            wrt.Flush();
            wrt.Close();

            // quick way to display the content of an XML file	    
            XmlReader reader = new XmlTextReader(url);
            reader.MoveToContent();
            StringBuilder sl = new StringBuilder();
            sl.Append(reader.ReadOuterXml());
            tbxQuo.Text = sl.ToString();
            reader.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://152.226.232.128/upload/Quotation.xml");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            //This example assumes the FTP sites uses abobymous logon.
            request.Credentials = new NetworkCredential("Sharon", "123456");

            //Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader(@"D:\GenerateXML\QuotationS2.xml");
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            MessageBox.Show("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();

        }
    }
}



